package czy.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@EnableSwagger2
@Configuration
public class SpringSwaggerConfig {

    private static final String NAME = "crayon-shin-chan";
    private static final String URL = "https://github.com/crayon-shin-chan";
    private static final String EMAIL = "1253665625@qq.com";
    private static final String VERSION = "1.0";
    private static final String LICENSE = "Apache License";


    /* API信息 */
    private static ApiInfo apiInfo(String title) {
        Contact contact = new Contact(NAME,URL,EMAIL);
        return new ApiInfoBuilder()
                /* API标题 */
                .title(title)
                /* 版本 */
                .version(VERSION)
                /* 许可 */
                .license(LICENSE)
                .contact(contact)
                .build();
    }

    /* 历史流程实例和历史任务、历史活动管理 */
    @Bean
    public Docket historyDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("history")
                .apiInfo(apiInfo("历史流程实例和历史任务、历史活动管理"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.history"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 用户、用户组身份管理 */
    @Bean
    public Docket indentityDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("indentity")
                .apiInfo(apiInfo("用户、用户组身份管理"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.indentity"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 管理接口 */
    @Bean
    public Docket managementDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("management")
                .apiInfo(apiInfo("管理接口"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.management"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 流程定义和部署存储库管理 */
    @Bean
    public Docket repositoryDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("repository")
                .apiInfo(apiInfo("流程部署和流程定义存储库管理"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.repository"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 运行时接口管理 */
    @Bean
    public Docket runtimeDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("runtime")
                .apiInfo(apiInfo("运行时接口管理"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.runtime"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 任务管理 */
    @Bean
    public Docket taskDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("task")
                .apiInfo(apiInfo("任务管理"))
                .select()
                .apis(RequestHandlerSelectors.basePackage("czy.demo.controller.task"))
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /* 安全模式，这里指定token通过Authorization头请求头传递 */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList= new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /* 安全上下文 */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.any())
                        .build());
        return securityContexts;
    }

    /* 默认的安全饮用 */
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences= new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

}
